package Manager

import (
	managerDao "deepgox-go-admin/Dao/Manager"
	managerModels "deepgox-go-admin/Models/Manager"
	"deepgox-go-admin/Pkg/HttpResponse"
	"deepgox-go-admin/Pkg/Jwt"
	"deepgox-go-admin/Pkg/Util"
	"errors"
	"fmt"
	"github.com/gin-gonic/gin"
	"github.com/go-playground/validator/v10"
	"strconv"
)

// LoginHandle 管理员登陆接口
//
//	@Summary		管理员登陆接口
//	@Description	管理员登陆接口
//	@Tags			管理员登陆接口
//	@Accept			application/json
//	@Produce		application/json
//	@Param			req	body	managerModels.LoginModelHandler	true	"req"
//	@type:			managerModels.LoginModelHandler
//	@Success		200	{object}	Response._ResponsePostList
//	@Router			/v1/manager/login [post]
func LoginHandle(c *gin.Context) {

	////获得post请求参数
	//b, _ := c.GetRawData()
	//// 定义map或结构体
	//var m map[string]interface{}
	//// 反序列化
	//_ = json.Unmarshal(b, &m)
	//fmt.Println(m["username"])
	//fmt.Println(m)
	//1.获取参数校验
	p := new(managerModels.LoginModelHandler)
	if err := c.ShouldBindJSON(&p); err != nil {
		var validationErrors validator.ValidationErrors
		if errors.As(err, &validationErrors) {
			erromap := Util.GetErrorMap(validationErrors)
			HttpResponse.ResponseErrorWithMsg(c, HttpResponse.CodeInvalidParams, Util.ToString(erromap))
		} else {
			HttpResponse.ResponseErrorWithMsg(c, HttpResponse.CodeInvalidParams, "参数格式错误")
		}
		return
	}
	if len(p.UserName) == 0 || len(p.Password) == 0 {
		//请求参数有误
		HttpResponse.ResponseErrorWithMsg(c, HttpResponse.CodeInvalidParams, "参数不能为空")
		return
	}
	////密码加密
	PW := Util.MD5(p.Password)
	managerModel := managerModels.LoginModelHandler{
		UserName: p.UserName,
		Password: PW,
	}
	//////////////////////////////////////////////////////////
	////验证码是否存在
	//if len(p.CheckCode) == 0 || len(p.CheckCodeId) == 0 {
	//	//请求参数有误
	//	HttpResponse.ResponseErrorWithMsg(c, HttpResponse.CodeInvalidParams, "验证码有误，请重新刷新")
	//	return
	//}
	////验证验证码是否正确
	//if !Util.VerifyCaptcha(p.CheckCodeId, p.CheckCode) {
	//	HttpResponse.ResponseErrorWithMsg(c, HttpResponse.CodeInvalidParams, "验证码有误，请重新刷新")
	//	return
	//}

	Manager := &managerDao.LoginModelHandler{managerModel}
	var result, err = Manager.LoginManager()
	if err != nil {
		HttpResponse.ResponseError(c, HttpResponse.CodeServerBusy)
		return
	}
	if result.Id == 0 {
		//请求参数有误
		HttpResponse.ResponseErrorWithMsg(c, HttpResponse.CodeInvalidParams, "帐号或者密码错误")
		return
	}
	var tokenString, _ = Jwt.GenToken(result.Id, result.UserName)
	HttpResponse.ResponseSuccess(c, gin.H{"token": tokenString, "managerid": result.Id})
}

// CheckCode 验证码获取
//
//	@Summary		验证码获取接口
//	@Description	验证码获取接口
//	@Tags			验证码获取接口
//	@Accept			application/json
//	@Produce		application/json
//	@Success		200	{object}	Response._ResponsePostList
//	@Router			/v1/manager/checkcode [get]
func CheckCode(c *gin.Context) {
	id, b64s, err := Util.MakeCaptcha()
	if err != nil {
		fmt.Println(err)
	}
	HttpResponse.ResponseSuccess(c, gin.H{
		"captchaId":    id,
		"captchaImage": b64s,
	})
}

func AddManager(c *gin.Context) {
	p := new(managerModels.AddModelHandler)
	if err := c.ShouldBindJSON(&p); err != nil {
		HttpResponse.ResponseErrorWithMsg(c, HttpResponse.CodeInvalidParams, "参数格式错误")
		return
	}
	if len(p.UserName) == 0 || len(strconv.FormatInt(p.Gender, 36)) == 0 || len(p.Password) == 0 {
		//请求参数有误
		HttpResponse.ResponseErrorWithMsg(c, HttpResponse.CodeInvalidParams, "参数不能为空")
		return
	}
	PW := Util.MD5(p.Password)
	manager := &managerDao.RegModelHandler{managerModels.AddModelHandler{
		UserName: p.UserName,
		Password: PW,
		Email:    p.Email,
		Gender:   p.Gender,
	}}
	result, err := manager.AddManager()
	if err != nil {
		HttpResponse.ResponseError(c, HttpResponse.CodeServerBusy)
		return
	}
	if result == 0 {
		HttpResponse.ResponseErrorWithMsg(c, HttpResponse.CodeFormatError, "添加用户失败")
		return
	}
	if result > 0 {
		HttpResponse.ResponseSuccess(c, result)
		return
	}
}
